<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use think\Log;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Borrows extends Backend
{
    
    /**
     * Borrow模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Borrow');

    }

    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)->with('user')
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)->with('user')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($list as $key=>$val){
                if($val['isDay']==1){
                    $list[$key]['borrowtime'] = $val['time'].'天';
                }else{
                    $list[$key]['borrowtime'] = $val['time'].'个月';
                }
                switch($val['type']){
                    case '1': $type='新手标';break;
                    case '2': $type='车信贷';break;
                    case '3': $type='车抵贷';break;
                    case '4': $type='银盈宝';break;
                    case '5': $type='助业贷';break;
                    case '6': $type='黄金宝';break;
                    default:  $type='';
                }


                $list[$key]['type1']  =$type;
                switch($val['status']) {
                    case '-2': $status = '流标';   break;
                    case '-1': $status = '审核未通过';   break;
                    case '0': $status = '未发布';   break;
                    case '1': $status = '投标中';   break;
                    case '2': $status = '满标待审';   break;
                    case '3': $status = '提交放款中';   break;
                    case '4': $status = '开始还款';   break;
                    case '5': $status = '还款完成';   break;
                    case '6': $status = '结束债券';   break;
                    default:  $status = '';
                }

                $count = Db::name('borrow_tender')->where('borrowId',$val['borrowId'])->count();
                $list[$key]['tendercount'] = $count;
                $list[$key]['status1']  =$status;
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);

        if($row['isDay']==1){
            $row['borrowtime'] = $row['time'].'天';
        }else{
            $row['borrowtime'] = $row['time'].'个月';
        }
        switch($row['type']){
            case '1': $type='新手标';break;
            case '2': $type='车信贷';break;
            case '3': $type='车抵贷';break;
            case '4': $type='银盈宝';break;
            case '5': $type='助业贷';break;
            case '6': $type='黄金宝';break;
            default:  $type='';
        }
        $row['type1']  =$type;
        switch($row['status']) {
            case '0': $status = '未发布';   break;
            case '1': $status = '投标中';   break;
            case '2': $status = '审核失败';   break;
            case '3': $status = '满标待审';   break;
            case '4': $status = '提交放款中';   break;
            case '5': $status = '开始还款';   break;
            case '6': $status = '还款完成';   break;
            case '7': $status = '结束债券';   break;
            default:  $status = '';
        }
        $row['status1']  =$status;

        $detail =  DB::name('borrow_detail')->where('borrowId',$row['borrowId'])->find();

        $detail['borrowerImg'] = explode(',',$detail['borrowerImgs']);
        $detail['itemImg'] = explode(',',$detail['itemImgs']);
        $detail['riskImg'] = explode(',',$detail['riskImgs']);
        $detail['projectImg'] = explode(',',$detail['projectImgs']);
        $detail['repaymentImg'] = explode(',',$detail['repaymentImgs']);

        $tender = Db::name('borrow_tender')->alias('t')->field('t.tenderId,t.account,t.createTime,u.phone')->join('user u','t.userId=u.userId')->where('t.borrowId',$row['borrowId'])->where('t.status >0')->order('t.tenderId asc')->select();
        $repay = DB::name('borrow_repay')->where('borrowId',$row['borrowId'])->order('repayId asc')->select();
        $this->view->assign("repay", $repay);
        $this->view->assign("tender", $tender);
        $this->view->assign("detail", $detail);
        $this->view->assign("row", $row);
        return $this->view->fetch('borrows/detail');
    }

}
